package com.wwwarehouse.contract.create_warehouse_map;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.GridView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.adapter.CreateWarehouseMapAdapter;
import com.wwwarehouse.contract.bean.CreateWarehouseMapTaskBean;
import com.wwwarehouse.contract.bean.ListCreateWarehouseMapBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/10/17.
 */

public class CreateWarehouseMapGridFragment extends BaseFragment {
	private List<ListCreateWarehouseMapBean.ListBean> list = new ArrayList<>();
	private CreateWarehouseMapAdapter createWarehouseMapAdapter;
	private GridView mGridView;
	private String mBusinessId;
	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.create_warehouse_map_grid_layout , container , false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mGridView = (GridView) view.findViewById(R.id.grid_view);
		mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//跳转到修改地图页面 带数据过去
				ListCreateWarehouseMapBean.ListBean listBean = list.get(position);
				Bundle bundle = new Bundle();
				bundle.putSerializable(ContractConstant.KEY_ALTER_WAREHOUSE_MAP_BEAN, listBean);
				bundle.putString(ContractConstant.KEY_CONTRACT_BUSINESSID, mBusinessId);
				AlterWarehouseMapFragment alterWarehouseMapFragment = new AlterWarehouseMapFragment();
				alterWarehouseMapFragment.setArguments(bundle);
				pushFragment(alterWarehouseMapFragment);
			}
		});
	}

	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		if (enter) {
			if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA) != null) {
				final ListCreateWarehouseMapBean listBean = (ListCreateWarehouseMapBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);
				if (listBean != null) {
					mBusinessId = listBean.getmBusinessId();
				}
				int position = getArguments().getInt(ContractConstant.KEY_BUNDLE_POSITION);
				int pageNum = getArguments().getInt(ContractConstant.KEY_BUNDLE_PAGENUM);
				list.clear();
				for (int i = position * pageNum; i < (position + 1) * pageNum; i++) {
					if (i >= 0 && i < listBean.getList().size()) {
						list.add(listBean.getList().get(i));
					}
				}
				mGridView.post(new Runnable() {
					@Override
					public void run() {
						createWarehouseMapAdapter = new CreateWarehouseMapAdapter(list, mActivity,mGridView.getHeight() - mGridView.getPaddingBottom() - mGridView.getPaddingTop() - mGridView.getVerticalSpacing() - 50);
						mGridView.setAdapter(createWarehouseMapAdapter);
					}
				});

//
			}
		}
		return null;
	}

	@Override
	public void setTitle() {

	}

	@Override
	public void requestDatas() {

	}
}
